<template>
	<view class="multi-channel-bg">
		<view class="welding-type-bg mar-bottom">
			<view class="task-title">类型：</view>
			<picker mode="selector" :range="multiTypeList" range-key="name" @change="onMultiTypeChange">
				<view class="picker">
					{{selectedMultiTypeBean ? selectedMultiTypeBean.name : '请选择类型'}}
				</view>
			</picker>
		</view>

		<view class="tab-bg">
			<view v-for="(item, index) in tabList" :key="index"
				:class="currentTabPosition==item.tabIndex?'tab-item-selected-bg':'tab-item-unselect-bg'"
				@click="chooseTab(item, index)">
				<view class="tab-txt">{{item.name}}</view>
			</view>
		</view>

		<view class="content-multi-bg">
			<view v-show="currentTabPosition==0">
				<view class="list-data-bg">
					<view class="table-item-common-bg table-title-bg">
						<view class="table-th1-bg">抬升</view>
						<view class="table-th2-bg">后退</view>
						<view class="table-th3-bg">工艺</view>
					</view>
					<view class="table-item-common-bg table-data-bg" v-for="(item, index) in viewShowChannelDataList"
						:key="index" :class="index==(viewShowChannelDataList.length-1)?'last-table-item-bg':''">
						<view class="table-td1-bg">
							<input placeholder="抬升" type="digit" v-model="item.lift" />
						</view>
						<view class="table-td2-bg">
							<input placeholder="后退" type="digit" v-model="item.back" />
						</view>
						<view class="table-td3-bg" @click="showPopup(item, index)">
							{{item.craftDto?item.craftDto.name:""}}
						</view>
					</view>
				</view>
			</view>

			<view v-show="currentTabPosition==1">
				<view class="whole-width-bg">
					<view class="btn1-bg mar-right" @click="startDrag">切换为手动</view>
					<view class="btn1-bg mar-left" @click="stopDrag">切换为自动</view>
				</view>
				<robotBottomMoveVue ref="refBottomMoveView" :stickyTop="dragTop"></robotBottomMoveVue>
			</view>
		</view>

		<view class="bottom-btn-bg">
			<view class="whole-width-bg mar-right">
				<view class="btn1-bg" @click="save">保存</view>
			</view>
			<view class="whole-width-bg mar-left">
				<view class="btn1-bg" @click="startWelding">开始焊接</view>
			</view>
		</view>

		<uni-popup ref="refPopupProcess" @maskClick="maskClick" type="bottom">
			<view class="popup-bg">
				<view class="top-title-bg">
					<view class="cancel-txt" @click="maskClick">取消</view>
					<view class="mid-title-txt">工艺</view>
					<view class="comfirm-txt" @click="confirmClick">确定</view>
				</view>

				<scroll-view scroll-y="true" class="sv-bg">
					<view class="data-item-bg" v-for="(item, index) in temDataList"
						:class="selectedProcessIndex==index?'data-item-select-bg':'data-item-unselect-bg'"
						@click="popupItemClick(item, index)">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import robotApi from '@/apis/robotApi';
	import * as Constants from '@/common/constants.js';
	import robotBottomMoveVue from './robot-bottom-move.vue';
	export default {
		name: "robot-multi-channel",
		components: {
			robotBottomMoveVue
		},
		props: {
			stickyTop: {
				type: Number,
				default: 84
			},
		},
		data() {
			return {
				multiTypeList: [{
						name: "二层三道",
						code: 0
					},
					{
						name: "三层六道",
						code: 1
					}
				],
				selectedMultiTypeBean: null,
				selectedMultiTypeIndex: -1,

				tabList: [{
						name: "多层多道",
						tabIndex: 0
					},
					{
						name: "取点",
						tabIndex: 1
					}
				],
				/**
				 * 当前选择的tab下标
				 */
				currentTabPosition: 0,

				dragTop: this.stickyTop,

				multiChannelDataList: [],
				originChannelDataList: [],
				viewShowChannelDataList: [],

				clickItem: null,
				clickIndex: -1,

				tempPopupChooseIndex: -1,

				selectedProcessIndex: -1,
				temDataList: [],
				
				multiLayerTrackId: "",
				isAddMulti: false,
				
				isSubmit: false,
			};
		},
		mounted() {
			this.selectedMultiTypeIndex = 0
			this.selectedMultiTypeBean = this.multiTypeList[this.selectedMultiTypeIndex]

			this.getTemplateData()
			this.getMultiData()
		},
		methods: {
			onMultiTypeChange(e) {
				this.selectedMultiTypeIndex = e.detail.value
				this.selectedMultiTypeBean = this.multiTypeList[this.selectedMultiTypeIndex]
				this.getMultiData()
			},

			chooseTab(item, index) {
				if (this.currentTabPosition != item.tabIndex) {
					this.currentTabPosition = item.tabIndex
				}
			},

			getTemplateData() {
				this.temDataList = []
				robotApi.getCraftList({})
					.then(res => {
						// console.log("工艺列表：" + JSON.stringify(res))
						if (res && res.length > 0) {
							this.temDataList = res
						}
					})
			},

			getMultiData() {
				if (this.selectedMultiTypeBean == null) {
					return
				}
				let data = {
					type: this.selectedMultiTypeBean.code
				}
				this.$refs.refBottomMoveView.init()
				this.isAddMulti = false
				this.multiChannelDataList = []
				this.originChannelDataList = []
				this.viewShowChannelDataList = []
				robotApi.getMultiChannel(data)
					.then(res => {
						if (res.code == 200 || res.code == "200") {
							if (res.data && res.data.channelCrafts && res.data.channelCrafts.length > 0) {
								for (let i = 0; i < res.data.channelCrafts.length; i++) {
									let bean = res.data.channelCrafts[i]
									if (bean.craftDto) {
										this.multiChannelDataList.push(bean)
									} else {
										this.isAddMulti = true
									}
								}
							} else {
								this.isAddMulti = true
							}
							console.log("this.isAddMulti-->" + this.isAddMulti)
							if (!this.isAddMulti) {
								this.multiLayerTrackId = res.data.multiLayerTrackId
								this.viewShowChannelDataList = JSON.parse(JSON.stringify(this.multiChannelDataList));
								this.originChannelDataList = JSON.parse(JSON.stringify(this.multiChannelDataList));
								this.parseMultiDataAndPointData(res.data.channelCrafts, res.data.multiLayerTrackPathPoints)
							} else {
								this.initEmptyMultiData()
							}
							console.log("this.viewShowChannelDataList-->" + JSON.stringify(this.viewShowChannelDataList))
							console.log("this.originChannelDataList =>" + JSON.stringify(this.originChannelDataList))
						}
					})
					.catch(error => {

					})
			},
			
			parseMultiDataAndPointData(channelCrafts, templatePathPoints) {
				let list = []
				for(let i = 0; i < templatePathPoints.length; i++) {
					let templateBean = templatePathPoints[i]
					let pointBean = {
						isFixed: false,
						pointType: templateBean.pointType
					}
					pointBean.name = this.getPointNameByPointType(templateBean.pointType)
					let data = {
						jogpos: {},
						xyzwpr: {}
					}
					data.jogpos = this.parseStringToObj(1, templateBean.jogpos)
					data.xyzwpr = this.parseStringToObj(2, templateBean.xyzwpr)
					pointBean.data = data
					list.push(pointBean)
				}
				console.log("list-->" + JSON.stringify(list))
				this.$refs.refBottomMoveView.parentSetListData(list)
			},
			getPointNameByPointType(pointType) {
				let pointName = ""
				switch(pointType) {
					case Constants.POINT_TYPE.SAFE_POINT:
						pointName = Constants.POINT_NAME.SAFE_POINT
						break;
					case Constants.POINT_TYPE.ARCING_POINT:
						pointName = Constants.POINT_NAME.ARCING_POINT
						break;
					case Constants.POINT_TYPE.INTERMEDIATE_POINT:
						pointName = Constants.POINT_NAME.INTERMEDIATE_POINT
						break;
					case Constants.POINT_TYPE.EXTINGUISHING_POINT:
						pointName = Constants.POINT_NAME.EXTINGUISHING_POINT
						break;
					case Constants.POINT_TYPE.TRANSITIONS_POINT:
						pointName = Constants.POINT_NAME.TRANSITIONS_POINT
						break;
					case Constants.POINT_TYPE.PHOTO_POSITION:
						pointName = Constants.POINT_NAME.PHOTO_POSITION
						break;
					case Constants.POINT_TYPE.PHOTO_SAFE_POINT:
						pointName = Constants.POINT_NAME.PHOTO_SAFE_POINT
						break;
				}
				return pointName
			},
			parseStringToObj(stringType, stringValue) {
				let obj = {}
				if (stringType == 1) {
					// 获取jogpos
					let array = stringValue.split(",")
					if (array.length == 6) {
						obj.j1 = array[0]
						obj.j2 = array[1]
						obj.j3 = array[2]
						obj.j4 = array[3]
						obj.j5 = array[4]
						obj.j6 = array[5]
					}
				} else if (stringType == 2) {
					// 获取xyzwpr
					let array = stringValue.split(",")
					if (array.length == 6) {
						obj.x = array[0]
						obj.y = array[1]
						obj.z = array[2]
						obj.w = array[3]
						obj.p = array[4]
						obj.r = array[5]
					}
				}
				return obj
			},

			initEmptyMultiData() {
				if (this.selectedMultiTypeBean == null) {
					return
				}
				this.multiChannelDataList = []
				if (this.selectedMultiTypeBean.code == 0) {
					this.multiChannelDataList = [{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						}
					]
				} else if (this.selectedMultiTypeBean.code == 1) {
					this.multiChannelDataList = [{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						},{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						},
						{
							lift: 0,
							back: 0,
							craftDto: null
						}
					]
				}
				this.originChannelDataList = [];
				this.viewShowChannelDataList = JSON.parse(JSON.stringify(this.multiChannelDataList));
			},

			showPopup(item, index) {
				console.log("this.originChannelDataList =>" + JSON.stringify(this.originChannelDataList))
				console.log("this.viewShowChannelDataList =>" + JSON.stringify(this.viewShowChannelDataList))
				if (item.craftDto) {
					this.getSelectProcessInList(item)
				}
				console.log("打开弹框时：选中的工艺是：" + this.selectedProcessIndex)
				this.clickItem = item
				this.clickIndex = index
				this.openPopup()
			},
			getSelectProcessInList(item) {
				for (let i = 0; i < this.temDataList.length; i++) {
					let processBean = this.temDataList[i]
					if (item.craftDto.number == processBean.number) {
						this.selectedProcessIndex = i;
						return
					}
				}
			},
			popupItemClick(item, index) {
				this.tempPopupChooseIndex = index
				this.selectedProcessIndex = index
			},
			confirmClick() {
				this.viewShowChannelDataList[this.clickIndex].craftDto = this.temDataList[this.selectedProcessIndex]
				this.maskClick()
			},
			maskClick() {
				this.tempPopupChooseIndex = -1
				this.selectedProcessIndex = -1
				this.clickItem = null
				this.clickIndex = -1
				this.closePopup()
			},
			openPopup() {
				this.$refs.refPopupProcess.open()
			},
			closePopup() {
				this.$refs.refPopupProcess.close()
			},

			save() {
				for (let i = 0; i < this.viewShowChannelDataList.length; i++) {
					let bean = this.viewShowChannelDataList[i]
					if (!bean.craftDto) {
						uni.showToast({
							title: "请完整选择工艺",
							position: "bottom"
						})
						return
					}
				}
				let dragList = this.$refs.refBottomMoveView.parentGetListData()
				console.log("dragList -->" + JSON.stringify(dragList))
				for (let i = 0; i < dragList.length; i++) {
					let pointBean = dragList[i]
					if (!pointBean.data) {
						uni.showToast({
							title: "请取完所有点",
							position: "bottom"
						})
						return
					}
				}
				let multiLayerTrackPathPoints = []
				for (let i = 0; i < dragList.length; i++) {
					let pointBean = dragList[i]
					let temBean = {
						pointType: pointBean.pointType,
						jogpos: this.parseObjectToString(1, pointBean.data.jogpos),
						xyzwpr: this.parseObjectToString(2, pointBean.data.xyzwpr),
						sort: i
					}
					multiLayerTrackPathPoints.push(temBean)
				}
				let channelCrafts = []
				for (let i = 0; i < this.viewShowChannelDataList.length; i++) {
					let bean = this.viewShowChannelDataList[i]
					let temBean = {
						craftDto: bean.craftDto,
						lift: bean.lift,
						back: bean.back,
						channelNumber: 0,
						craftNumber: bean.craftDto.number
					}
					channelCrafts.push(temBean)
				}
				let data = {
					type: this.selectedMultiTypeBean.code,
					multiLayerTrackPathPoints: multiLayerTrackPathPoints,
					channelCrafts: channelCrafts
				}
				if (this.isAddMulti) {
					if (this.isSubmit) {
						return
					}
					this.isSubmit = true
					robotApi.saveMultiChannel(data)
						.then(res => {
							console.log("新增成功")
							console.log("新增成功 res-->" + JSON.stringify(res))
							if (typeof(res.data) === 'boolean' && res.data && (res.code == 200 || res.code == "200")) {
								uni.showToast({
									title: "新增成功"
								})
								this.getMultiData()
							} else {
								uni.showToast({
									title: res.msg
								})
							}
							this.isSubmit = false
						})
						.catch(error => {
							this.isSubmit = false
						})
				} else {
					data.multiLayerTrackId = this.multiLayerTrackId
					if (this.isSubmit) {
						return
					}
					this.isSubmit = true
					robotApi.updateMultiChannel(data)
						.then(res => {
							console.log("更新成功 res-->" + JSON.stringify(res))
							if (typeof(res.data) === 'boolean' && res.data && (res.code == 200 || res.code == "200")) {
								uni.showToast({
									title: "更新成功"
								})
								this.getMultiData()
							} else {
								uni.showToast({
									title: res.msg
								})
							}
							this.isSubmit = false
						})
						.catch(error => {
							this.isSubmit = false
						})
				}
			},

			startWelding() {
				if (!this.selectedMultiTypeBean) {
					uni.showToast({
						title: "请先选择类型"
					})
					return
				}
				if (this.isAddMulti || !this.originChannelDataList || this.originChannelDataList.length<=0) {
					uni.showToast({
						title: "请先保存",
						position: "bottom"
					})
					return
				}
				let dragList = this.$refs.refBottomMoveView.parentGetListData()
				console.log("dragList -->" + JSON.stringify(dragList))
				for (let i = 0; i < dragList.length; i++) {
					let pointBean = dragList[i]
					if (!pointBean.data) {
						uni.showToast({
							title: "请取完所有点",
							position: "bottom"
						})
						return
					}
				}
				let data = {
					type: this.selectedMultiTypeBean.code
				}
				if (this.isSubmit) {
					return
				}
				this.isSubmit = true
				robotApi.multiChannelWelding(data)
					.then(res => {
						if (typeof(res.data) === 'boolean' && res.data && (res.code == 200 || res.code == "200")) {
							uni.showToast({
								title: res.msg?res.msg:'操作成功'
							})
						} else {
							uni.showToast({
								title: res.msg
							})
						}
						this.isSubmit = false
					})
					.catch(error => {
						this.isSubmit = false
					})
			},
			parseObjectToString(type, value) {
				let res = ""
				if (type == 1) {
					res = value.j1 + "," + value.j2 + "," + value.j3 + "," + value.j4 + "," + value.j5 + "," + value.j6
				} else if (type == 2) {
					res = value.x + "," + value.y + "," + value.z + "," + value.w + "," + value.p + "," + value.r
				}
				return res
			},
		}
	}
</script>

<style scoped lang="scss">
	.multi-channel-bg {
		background-color: #1e40af;
		padding: 40rpx 40rpx 0 40rpx;
	}

	.welding-type-bg {
		display: flex;
		align-items: center;

		.picker-bg {
			flex: 1;
		}

		input,
		.picker {
			width: 500rpx;
			// flex: 1;
			padding: 10px;
			border: 1px solid rgba(37, 99, 235, 1);
			border-radius: 6px;
			font-size: 16px;
			background-color: #1e40af;
			color: #FFFFFF;
		}
	}

	.task-title {
		font-size: 32rpx;
		color: #FFFFFF;
		margin-right: 20rpx;
	}

	.tab-bg {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
		height: 80rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.tab-item-selected-bg {
		padding: 0 20rpx 20rpx 20rpx;
		border-bottom: 2rpx solid rgba(59, 130, 246, 1);
		color: #FFFFFF;
		font-size: 36rpx;
	}

	.tab-item-unselect-bg {
		padding: 0 20rpx 20rpx 20rpx;
		color: rgba(148, 163, 184, 1);
		font-size: 30rpx;
	}

	.content-multi-bg {
		padding-bottom: 100px;
	}

	.bottom-btn-bg {
		position: fixed;
		bottom: 0;
		right: 0;
		left: 0;
		padding: 0 20px 20px;
		background-color: #1e40af;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 9;

		.whole-width-bg {
			display: flex;
			align-items: center;
			width: 100%;
			margin-bottom: 30rpx;
		}

		.btn1-bg {
			padding: 20rpx 0;
			flex: 1;
			border-radius: 16rpx;
			background-color: #3b82f6;
			color: #FFFFFF;
			text-align: center;
		}

		.mar-left {
			margin-left: 20rpx;
		}

		.mar-right {
			margin-right: 20rpx;
		}
	}

	.list-data-bg {
		border: 2rpx solid rgba(59, 95, 159, 1);
		border-radius: 16rpx;
	}

	.table-item-common-bg {
		display: flex;
		align-items: center;
		padding: 20rpx;
	}

	.table-title-bg {
		border-top-right-radius: 16rpx;
		border-top-left-radius: 16rpx;
		background-color: #1e3a8a;
	}

	.table-th1-bg {
		flex: 3;
		color: #FFFFFF;
		font-size: 28rpx;
	}

	.table-th2-bg {
		flex: 4;
		color: #FFFFFF;
		font-size: 28rpx;
	}

	.table-th3-bg {
		flex: 3;
		color: #FFFFFF;
		font-size: 28rpx;
	}

	.table-data-bg {
		background-color: #1e40af;
		border-bottom: 2rpx solid #1e3a8a;
	}

	.last-table-item-bg {
		border-bottom-left-radius: 16rpx;
		border-bottom-right-radius: 16rpx;
	}

	.table-td1-bg {
		flex: 3;
		color: #FFFFFF;
		font-size: 28rpx;
	}

	.table-td2-bg {
		flex: 4;
		color: #FFFFFF;
		font-size: 28rpx;
	}

	.table-td3-bg {
		flex: 3;
		color: #FFFFFF;
		font-size: 28rpx;
		height: 60rpx;
	}

	.whole-width-bg {
		display: flex;
		align-items: center;
		width: 100%;
		margin-bottom: 30rpx;
	}

	.btn1-bg {
		padding: 20rpx 0;
		flex: 1;
		border-radius: 16rpx;
		background-color: #3b82f6;
		color: #FFFFFF;
		text-align: center;
	}

	.mar-left {
		margin-left: 20rpx;
	}

	.mar-right {
		margin-right: 20rpx;
	}

	.popup-bg {
		background-color: #FFFFFF;
		width: 100%;

		.top-title-bg {
			display: flex;
			align-items: center;
			background-color: #1e3a8a;
			padding: 20rpx 30rpx;

			.cancel-txt {
				color: #FFFFFF;
				font-size: 32rpx;
			}

			.mid-title-txt {
				color: #FFFFFF;
				font-size: 32rpx;
				flex: 1;
				text-align: center;
			}

			.comfirm-txt {
				font-size: 32rpx;
				color: #3b82f6;
			}
		}

		.sv-bg {
			max-height: 400rpx;
		}

		.data-item-bg {
			padding: 10rpx;
			border-bottom: 2rpx solid #e5e5e5;
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 32rpx;
		}

		.data-item-select-bg {
			color: #fff;
			background-color: #1e40af;
		}

		.data-item-unselect-bg {
			color: #000000;
			background-color: #FFFFFF;
		}
	}
</style>